package com.six.service;

import com.six.domain.entity.Store;
import com.six.domain.entity.User;
import com.six.mapper.StoreMapper;
import com.six.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.nio.file.AccessDeniedException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserService {

    private final UserMapper userMapper;
    private final StoreMapper storeMapper;


    public boolean isFirstLogin(Long userId) {
        // 简单判断：密码是否等于默认随机密码（可扩展）
        return false; // 留给你扩展
    }

    public void updatePassword(Long userId, String newPassword) {
        userMapper.updatePassword(userId, newPassword);
    }
    /**
     * 按门店查服务员
     * @param storeId  传 null 时返回全部（管理员用）
     * @return 过滤后的服务员列表
     */
    public List<User> listWaitersByStore(Long storeId, Authentication auth) throws AccessDeniedException {
        String username = auth.getName();                  // ✅ 正确拼写
        User currentUser = userMapper.selectByUsername(username);
        if (currentUser.getRoleId() != 3 && currentUser.getRoleId() != 4) {
            throw new AccessDeniedException("无权限查看门店员工");
        }

        Store store = storeMapper.selectByManagerId(currentUser.getId()); // ✅ 正确方法名
        if (store == null || !store.getId().equals(storeId)) {
            throw new AccessDeniedException("无权查看其它门店数据");
        }

        return userMapper.listWaitersByStore(storeId);
    }
    public List<User> listAllWaiters() {
        return userMapper.listAllWaiters();
    }
    public List<User> listWaitersByStore(Long storeId) {
        return userMapper.listWaitersByStore(storeId);
    }
}